using System.ComponentModel.DataAnnotations;

namespace DaoHang.Models
{
    /// <summary>
    /// 数据库配置模型
    /// </summary>
    public class DatabaseConfig
    {
        /// <summary>
        /// 数据库类型枚举
        /// </summary>
        public enum DatabaseType
        {
            InMemory = 0,
            MySQL = 1,
            SQLite = 2,
            SqlServer = 3
        }

        /// <summary>
        /// 选择的数据库类型
        /// </summary>
        [Required(ErrorMessage = "请选择数据库类型")]
        public DatabaseType Type { get; set; } = DatabaseType.InMemory;

        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        public string ConnectionString { get; set; } = string.Empty;

        /// <summary>
        /// MySQL服务器地址
        /// </summary>
        public string MySqlServer { get; set; } = "localhost";

        /// <summary>
        /// MySQL端口
        /// </summary>
        public int MySqlPort { get; set; } = 3306;

        /// <summary>
        /// MySQL数据库名
        /// </summary>
        public string MySqlDatabase { get; set; } = "daohang_db";

        /// <summary>
        /// MySQL用户名
        /// </summary>
        public string MySqlUsername { get; set; } = "root";

        /// <summary>
        /// MySQL密码
        /// </summary>
        public string MySqlPassword { get; set; } = string.Empty;

        /// <summary>
        /// SQLite数据库文件路径
        /// </summary>
        public string SQLiteFilePath { get; set; } = "daohang.db";

        /// <summary>
        /// SQL Server服务器地址
        /// </summary>
        public string SqlServerServer { get; set; } = "(localdb)\\MSSQLLocalDB";

        /// <summary>
        /// SQL Server数据库名
        /// </summary>
        public string SqlServerDatabase { get; set; } = "DaoHangDB";

        /// <summary>
        /// SQL Server用户名（可选，Windows认证时为空）
        /// </summary>
        public string SqlServerUsername { get; set; } = string.Empty;

        /// <summary>
        /// SQL Server密码（可选，Windows认证时为空）
        /// </summary>
        public string SqlServerPassword { get; set; } = string.Empty;

        /// <summary>
        /// 是否使用Windows认证（仅SQL Server）
        /// </summary>
        public bool UseWindowsAuthentication { get; set; } = true;

        /// <summary>
        /// 生成连接字符串
        /// </summary>
        /// <returns>连接字符串</returns>
        public string GenerateConnectionString()
        {
            return Type switch
            {
                DatabaseType.MySQL => $"Server={MySqlServer};Port={MySqlPort};Database={MySqlDatabase};User={MySqlUsername};Password={MySqlPassword};",
                DatabaseType.SQLite => $"Data Source={SQLiteFilePath}",
                DatabaseType.SqlServer when UseWindowsAuthentication => $"Server={SqlServerServer};Database={SqlServerDatabase};Trusted_Connection=true;TrustServerCertificate=true;",
                DatabaseType.SqlServer => $"Server={SqlServerServer};Database={SqlServerDatabase};User Id={SqlServerUsername};Password={SqlServerPassword};TrustServerCertificate=true;",
                DatabaseType.InMemory => "InMemory",
                _ => string.Empty
            };
        }

        /// <summary>
        /// 获取数据库类型显示名称
        /// </summary>
        /// <returns>显示名称</returns>
        public string GetDisplayName()
        {
            return Type switch
            {
                DatabaseType.InMemory => "内存数据库",
                DatabaseType.MySQL => "MySQL",
                DatabaseType.SQLite => "SQLite",
                DatabaseType.SqlServer => "SQL Server",
                _ => "未知"
            };
        }

        /// <summary>
        /// 获取数据库类型描述
        /// </summary>
        /// <returns>描述</returns>
        public string GetDescription()
        {
            return Type switch
            {
                DatabaseType.InMemory => "数据存储在内存中，重启后数据将丢失。适用于测试和演示。",
                DatabaseType.MySQL => "开源的关系型数据库，性能优异，适用于生产环境。",
                DatabaseType.SQLite => "轻量级的文件数据库，无需安装，适用于小型应用。",
                DatabaseType.SqlServer => "Microsoft的企业级数据库，功能强大，适用于大型应用。",
                _ => ""
            };
        }
    }
} 